package com.javajun.service;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.javajun.custom.BaseService;
import com.javajun.entity.Claxx;
import com.javajun.entity.Manager;
import com.javajun.repository.ClaxxRepository;
import com.javajun.repository.ManagerRepository;

@Service
public class ManagerService extends BaseService<Manager, Integer>{

	@Autowired
	private ManagerRepository managerRepository;
	
	// 动态安全查询
	public Specification<Manager> BuildSpec(Manager entity) {
		Specification<Manager> spec = new Specification<Manager>() {

			@Override
			public Predicate toPredicate(Root<Manager> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicates = new ArrayList<>();
				predicates.add(cb.greaterThan(root.get("id"), 0));
				if (StringUtils.hasText(entity.getName())) {
					predicates.add(cb.like(root.get("name"), "%" + entity.getName() + "%"));
				}
				if (StringUtils.hasText(entity.getSex())) {
					predicates.add(cb.equal(root.get("sex"), entity.getSex()));
				}
				return cb.and(predicates.toArray(new Predicate[predicates.size()]));
			}
		};
		return spec;
	}
	
}
